package com.itshidu.game.controller


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.itshidu.game.mapper.FightLogMapper
import com.itshidu.game.mapper.HeroItemMapper
import com.itshidu.game.mapper.HeroMapper
import com.itshidu.game.mapper.ItemMapper
import com.itshidu.game.mapper.UserMapper
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping

@Controller
@RequestMapping("/game/items")
class GameItemController extends BaseController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    HeroMapper heroMapper;
    @Autowired FightLogMapper fightLogMapper

    @Autowired HeroItemMapper heroItemMapper
    @Autowired ItemMapper itemMapper


    @RequestMapping("/{itemId}/use")
    @Transactional
    items_use(Model model,@PathVariable int itemId) {
        def hero = heroMapper.selectById(getCurrentHero().id)
        def heroItem = heroItemMapper.selectById(itemId)

        println ('使用物品：'+itemId)
        if (heroItem == null) {
            return 'redirect:/game/items/index'
        }
        def item = itemMapper.selectById(heroItem.itemId)
        heroItem._item = item
        if (item == null || !item.usable) {
            return 'redirect:/game/items/index'
        }

        //消耗掉
        if (heroItem.quantity > 1) {
            heroItem.quantity--;
            heroItemMapper.updateById(heroItem)
        }else{
            heroItemMapper.deleteById(heroItem.id)
        }


        /* 属性洗练丹 */
        if (heroItem.itemId == 1) {

            hero.ap += hero.apCritVal
            hero.critVal -= hero.apCritVal
            hero.apCritVal = 0

            hero.ap += hero.apCritRate
            hero.critRate -= hero.apCritRate
            hero.apCritRate = 0

            hero.ap += hero.apAgile
            hero.agile -= hero.apAgile
            hero.apAgile = 0

            hero.ap += hero.apDodge
            hero.dodge -= hero.apDodge
            hero.apDodge = 0

            hero.ap += hero.apHit
            hero.hit -= hero.apHit
            hero.apHit = 0

            heroMapper.updateById(hero)

            return 'redirect:/game/items/index'
        }

        /* 潜龙经验丹 */
        if (heroItem.itemId == 3) {
            hero.exp += 2000
            heroMapper.updateById(hero)
            heroItemMapper.deleteById(heroItem)
        }

        return 'redirect:/game/items/index'
    }

    @RequestMapping("/index")
    item_list(Model model) {
        def hero = heroMapper.selectById(getCurrentHero().id)
        def wrapper = new QueryWrapper()
        wrapper.eq('hero_id',hero.id)
        def list = heroItemMapper.selectList(wrapper)
        list.forEach({hi->
            hi._hero = heroMapper.selectById(hi.heroId)
            hi._item = itemMapper.selectById(hi.itemId)
        })
        model.addAttribute('list',list)
        return "game/item_index";
    }

}
